package com.ten.mall.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.ItemService;
import com.ten.mall.service.LunboService;
import com.ten.mall.vo.Item;
import com.ten.mall.vo.Lunbo;

@Controller
public class LunboController {
	@Autowired
	private LunboService lunboService;
	@Autowired
	private ItemService itemService;
	@RequestMapping("/admin/lunboList")
	public String lunboList(HttpServletRequest res,String name){
		 List<Lunbo> list =lunboService.selectAll();
		 res.setAttribute("list", list);
		 return "jsp/lunboList";
	 }
	@RequestMapping("/admin/addLunbo")
	public String addLunbo(HttpServletRequest res,Integer id){
		List<Item> list =itemService.selectAll();
		res.setAttribute("list", list);
		if(id!=null){
			Lunbo lunbo =lunboService.selectById(id);
			res.setAttribute("lunbo", lunbo);
		}
		return "jsp/lunboAdd";
	}
	 @RequestMapping("/admin/saveLunbo")
	 @ResponseBody
	 public String saveLunbo(Lunbo lunbo){
		  String rs="error";
		  try {
			if(lunbo.getId()!=null){
				lunboService.updateLunbo(lunbo);
			}else{
				lunboService.insertLunbo(lunbo);
				}
			rs="ok";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  return rs;
	  }
	  @RequestMapping("/admin/lunboDel")
	  @ResponseBody
		public String lunboDel(Integer id){
			String ss="ok";
			try {
				if(id!=null){
					lunboService.deleteLunbo(id);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ss="error";
			}
			return ss;
		}
}
